package com.augurit.wf.common.tag.base;

import java.util.Map;
import java.lang.RuntimeException;

import javax.servlet.ServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.components.Component;
import org.springside.modules.utils.SpringContextHolder;

import com.augurit.wf.bi.service.IWfActElemPrivDefService;
import com.augurit.wf.bi.web.form.WfActElemPrivDefForm;
import com.augurit.wf.common.listener.WfInitListener;
import com.augurit.wf.common.util.WfUtils;
import com.opensymphony.xwork2.util.ValueStack;

/**
 * 
 * 
 */
public class WfPageElemWithPrivImpl extends Component implements IWfPageElemWithPriv{
	private String id;
	
	private boolean isHiddenByPriv;		//是否是被权限隐藏
	private boolean isDisabledByPriv;	//是否是被权限锁定
	
	public boolean isHiddenByPriv() {
		return isHiddenByPriv;
	}

	public void setHiddenByPriv(boolean isHiddenByPriv) {
		this.isHiddenByPriv = isHiddenByPriv;
	}

	public boolean isDisabledByPriv() {
		return isDisabledByPriv;
	}

	public void setDisabledByPriv(boolean isDisabledByPriv) {
		this.isDisabledByPriv = isDisabledByPriv;
	}

	public WfPageElemWithPrivImpl(ValueStack arg0){
		super(arg0);
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}
	
	
	/**
	 * 根据页面元素id设置元素权限
	 * @param elementId 页面元素id
	 * @param req
	 */
	public void setPrivByElementId(String elementId, ServletRequest req){
		String templateCode = req.getParameter("templateCode");
		String version = req.getParameter("version");
		String viewName = req.getParameter("viewName");
		String activityName = req.getParameter("activityName");
		
		if(templateCode == null || templateCode.trim().length() == 0 || version == null || viewName == null && viewName.trim().length() == 0){
			throw new RuntimeException("templateCode, version, viewName is null!");
		}
		if(viewName.equals(WfActElemPrivDefForm.DB_ZB_VIEW) && activityName == null){
			throw new RuntimeException("Db、Zb View must be given activityName!");
		}
		
		@SuppressWarnings("unchecked")
		Map<String, WfActElemPrivDefForm> map = (Map<String, WfActElemPrivDefForm>)ServletActionContext.getServletContext().getAttribute(WfInitListener.PRIV_CACHE);
		if(map == null){
			throw new RuntimeException("Can't get priv control information from cache!");
		}
		
		String key = WfUtils.formatPrivControlKey(elementId, templateCode, version, viewName, activityName);
		
		//从缓存中获取权限信息
		WfActElemPrivDefForm form = map.get(key);
		//缓存中不存在，从数据库中获取
		if(form == null){
			IWfActElemPrivDefService wfActElemPrivDefService = SpringContextHolder.getApplicationContext().getBean(IWfActElemPrivDefService.class);
			form = wfActElemPrivDefService.getElemPriv(templateCode, Integer.parseInt(version), viewName, activityName, elementId);
		}
		
		//从缓存或者数据库中获取到权限信息
		if(form != null){
			this.setHiddenByPriv(form.isHidden());
			this.setDisabledByPriv(form.isLocked());
		}
		//没有该页面元素的权限控制信息
		else{
			WfActElemPrivDefForm virturePrivform = new WfActElemPrivDefForm();
			virturePrivform.setHidden(false);
			virturePrivform.setLocked(false);
			map.put(key, virturePrivform);
		}
	}
}
